<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="一.axios 的基本使用axios发送get请求&#x2F;&#x2F;默认请求方式发送无参请求&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;&lt;script&gt;    axios(&amp;#123">
<meta property="og:type" content="article">
<meta property="og:title" content="axios基础">
<meta property="og:url" content="http://example.com/2021/01/28/axios/index.html">
<meta property="og:site_name" content="Lawted&#39;s Blog">
<meta property="og:description" content="一.axios 的基本使用axios发送get请求&#x2F;&#x2F;默认请求方式发送无参请求&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;&lt;script&gt;    axios(&amp;#123">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-28T03:23:46.000Z">
<meta property="article:modified_time" content="2021-01-28T03:25:31.390Z">
<meta property="article:author" content="Lawted Wu">
<meta property="article:tag" content="技术">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/01/28/axios/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>
<title>axios基础 | Lawted's Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lawted's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80-axios-%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-number">1.</span> <span class="nav-text">一.axios 的基本使用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#axios%E5%8F%91%E9%80%81get%E8%AF%B7%E6%B1%82"><span class="nav-number">1.0.1.</span> <span class="nav-text">axios发送get请求</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#axios%E5%8F%91%E9%80%81post%E8%AF%B7%E6%B1%82"><span class="nav-number">1.0.2.</span> <span class="nav-text">axios发送post请求</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C-axios%E8%AF%B7%E6%B1%82%E6%96%B9%E5%BC%8F"><span class="nav-number">2.</span> <span class="nav-text">二.axios请求方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89-axios%E7%9A%84%E5%B9%B6%E5%8F%91%E8%AF%B7%E6%B1%82"><span class="nav-number">3.</span> <span class="nav-text">三.axios的并发请求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%9B-axios%E7%9A%84%E5%85%A8%E5%B1%80%E9%85%8D%E7%BD%AE"><span class="nav-number">4.</span> <span class="nav-text">四.axios的全局配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%94-axios%E7%9A%84%E5%AE%9E%E4%BE%8B"><span class="nav-number">5.</span> <span class="nav-text">五.axios的实例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%AD-axios%E7%9A%84%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="nav-number">6.</span> <span class="nav-text">六.axios的拦截器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%83-axios%E5%9C%A8Vue%E4%B8%AD%E7%9A%84%E6%A8%A1%E5%9D%97%E5%B0%81%E8%A3%85"><span class="nav-number">7.</span> <span class="nav-text">七.axios在Vue中的模块封装</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%9C%80%E5%A5%BD%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">7.0.0.1.</span> <span class="nav-text">最好的方法</span></a></li></ol></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Lawted Wu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LAWTED" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LAWTED" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/01/28/axios/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          axios基础
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>
      

      <time title="Created: 2021-01-28 11:23:46 / Modified: 11:25:31" itemprop="dateCreated datePublished" datetime="2021-01-28T11:23:46+08:00">2021-01-28</time>
    </span>

  
    <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">Views: </span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="一-axios-的基本使用"><a href="#一-axios-的基本使用" class="headerlink" title="一.axios 的基本使用"></a>一.axios 的基本使用</h2><h4 id="axios发送get请求"><a href="#axios发送get请求" class="headerlink" title="axios发送get请求"></a>axios发送get请求</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#x2F;&#x2F;默认请求方式发送无参请求</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios(&#123;</span><br><span class="line">        url:&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;posts&#39;,</span><br><span class="line">    &#125;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line">默认使用get方式请求</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#x2F;&#x2F;指定请求方式为get发送无参请求</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios(&#123;</span><br><span class="line">        url:&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;posts&#39;,</span><br><span class="line">        method:&#39;get&#39;</span><br><span class="line">    &#125;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#x2F;&#x2F;发送get方式有参请求-直接在url后面拼接?id&#x3D;1</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios(&#123;</span><br><span class="line">        url:&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments?id&#x3D;1&#39;,</span><br><span class="line">    &#125;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#x2F;&#x2F;发送get方式有参请求-使用params</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios(&#123;</span><br><span class="line">        url:&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments&#39;,</span><br><span class="line">        params:&#123;</span><br><span class="line">            id:&#39;1&#39;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<h4 id="axios发送post请求"><a href="#axios发送post请求" class="headerlink" title="axios发送post请求"></a>axios发送post请求</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#x2F;&#x2F;指定请求方式为post发送无参请求</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios(&#123;</span><br><span class="line">        url:&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments&#39;,</span><br><span class="line">        method:&#39;post&#39;</span><br><span class="line">    &#125;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios(&#123;</span><br><span class="line">        url:&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments&#39;,</span><br><span class="line">        method:&#39;post&#39;,</span><br><span class="line">        data:&#123;</span><br><span class="line">            id:&#39;3&#39;,</span><br><span class="line">            name:&#39;科技新闻&#39;,</span><br><span class="line">            post: &quot;2&quot;,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="二-axios请求方式"><a href="#二-axios请求方式" class="headerlink" title="二.axios请求方式"></a>二.axios请求方式</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#x2F;&#x2F;使用axios.get发送无参请求</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios.get(&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments&#39;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;).catch(err&#x3D;&gt;&#123;</span><br><span class="line">        console.log(err);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#x2F;&#x2F;使用axios.get发送有参请求</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios.get(&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments&#39;,&#123;params:&#123;id:2&#125;&#125;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;).catch(err&#x3D;&gt;&#123;</span><br><span class="line">        console.log(err);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#x2F;&#x2F;使用axios.post发送无参请求</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios.post(&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments&#39;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;).catch(err&#x3D;&gt;&#123;</span><br><span class="line">        console.log(err);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#x2F;&#x2F;使用axios.post发送有参请求</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios.post(&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments&#39;,&#123;id:4,body:&quot;时尚新闻&quot;&#125;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;).catch(err&#x3D;&gt;&#123;</span><br><span class="line">        console.log(err);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line">or</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios.post(&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments&#39;,&quot;id&#x3D;4&amp;body&#x3D;时尚新闻&quot;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;).catch(err&#x3D;&gt;&#123;</span><br><span class="line">        console.log(err);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>axios使用post携带参数默认使用application/json</p>
</blockquote>
<p>解决方法一：params属性进行数据传递</p>
<p>解决方法二：“name=张三&amp;id=1”</p>
<p>解决方法三：服务端给接受到的参数加上@requestbody</p>
<hr>
<h2 id="三-axios的并发请求"><a href="#三-axios的并发请求" class="headerlink" title="三.axios的并发请求"></a>三.axios的并发请求</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios.all([</span><br><span class="line">        axios.get(&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments&#39;),</span><br><span class="line">        axios.get(&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;posts&#39;)</span><br><span class="line">    ]).then(</span><br><span class="line">        axios.spread((res1,res2)&#x3D;&gt;&#123;</span><br><span class="line">            console.log(res1);</span><br><span class="line">            console.log(res2);</span><br><span class="line">        &#125;)</span><br><span class="line">    ).catch(err&#x3D;&gt;&#123;</span><br><span class="line">        console.log(err);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios.all([</span><br><span class="line">        axios.get(&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;comments&#39;),</span><br><span class="line">        axios.get(&#39;http:&#x2F;&#x2F;localhost:3008&#x2F;posts&#39;)</span><br><span class="line">    ]).then(res&#x3D;&gt;&#123;</span><br><span class="line">            console.log(res[0]);</span><br><span class="line">            console.log(&quot;----------&quot;)</span><br><span class="line">            console.log(res[1]);</span><br><span class="line">        &#125;</span><br><span class="line">    ).catch(err&#x3D;&gt;&#123;</span><br><span class="line">        console.log(err);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="四-axios的全局配置"><a href="#四-axios的全局配置" class="headerlink" title="四.axios的全局配置"></a>四.axios的全局配置</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios.defaults.baseURL&#x3D;&#39;http:&#x2F;&#x2F;localhost:3008&#39;;</span><br><span class="line">    &#x2F;&#x2F;axios.defaults.timeout&#x3D;5000;</span><br><span class="line">    axios.get(&#39;comments&#39;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;);</span><br><span class="line">    axios.get(&#39;posts&#39;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="五-axios的实例"><a href="#五-axios的实例" class="headerlink" title="五.axios的实例"></a>五.axios的实例</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    let newVar &#x3D; axios.create(&#123;</span><br><span class="line">        baseURL:&#39;http:&#x2F;&#x2F;localhost:3008&#39;,</span><br><span class="line">        &#x2F;&#x2F;timeout:5000</span><br><span class="line">    &#125;);&#x2F;&#x2F;创建axios的实例</span><br><span class="line">    newVar(&#123;</span><br><span class="line">        url:&#39;comments&#39;</span><br><span class="line">    &#125;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="六-axios的拦截器"><a href="#六-axios的拦截器" class="headerlink" title="六.axios的拦截器"></a>六.axios的拦截器</h2><figure class="highlight txt"><table><tr><td class="code"><pre><span class="line">axios 给我提供了两大拦截器 </span><br><span class="line">一种是请求方向的拦截（成功请求，失败请求）</span><br><span class="line">一种是响应方向的（成功响应，失败响应）</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios.interceptors.request.use(config&#x3D;&gt;&#123;</span><br><span class="line">        console.log(config);&#x2F;&#x2F;可以在这里加载动画</span><br><span class="line">        return config;</span><br><span class="line">    &#125;,err&#x3D;&gt;&#123;</span><br><span class="line">        console.log(err);</span><br><span class="line">    &#125;)</span><br><span class="line">    axios.get(&quot;http:&#x2F;&#x2F;localhost:3008&#x2F;posts&quot;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;unpkg.com&#x2F;axios&#x2F;dist&#x2F;axios.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    axios.interceptors.response.use(config&#x3D;&gt;&#123;</span><br><span class="line">        console.log(config);&#x2F;&#x2F;可以在这里加载动画</span><br><span class="line">        return config.data;&#x2F;&#x2F;拦截后放行</span><br><span class="line">    &#125;,err&#x3D;&gt;&#123;</span><br><span class="line">        console.log(err);</span><br><span class="line"></span><br><span class="line">    &#125;)</span><br><span class="line">    axios.get(&quot;http:&#x2F;&#x2F;localhost:3008&#x2F;posts&quot;).then(res&#x3D;&gt;&#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="七-axios在Vue中的模块封装"><a href="#七-axios在Vue中的模块封装" class="headerlink" title="七.axios在Vue中的模块封装"></a>七.axios在Vue中的模块封装</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//封装位置 /src/network/request/request.js</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&quot;axios&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">request</span>(<span class="params">config,success,fail</span>)</span>&#123;</span><br><span class="line">  axios(&#123;</span><br><span class="line">    url:config</span><br><span class="line">  &#125;).then(<span class="function"><span class="params">res</span>=&gt;</span>&#123;</span><br><span class="line">    success(res);</span><br><span class="line">  &#125;).catch(<span class="function"><span class="params">err</span>=&gt;</span>&#123;</span><br><span class="line">    fail(err);</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//调用位置</span></span><br><span class="line"><span class="keyword">import</span> &#123;request&#125; <span class="keyword">from</span> <span class="string">&quot;./network/request/request&quot;</span>;</span><br><span class="line">request(</span><br><span class="line">  <span class="string">&quot;http://localhost:3008/comments&quot;</span>,</span><br><span class="line">  res=&gt;&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(res);</span><br><span class="line">  &#125;,</span><br><span class="line">  res=&gt;&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">  &#125;</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//封装位置</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&quot;axios&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">request</span>(<span class="params">config</span>)</span>&#123;</span><br><span class="line">axios.defaults.baseURL=<span class="string">&quot;http://localhost:3008&quot;</span></span><br><span class="line">  axios(config.url).then(</span><br><span class="line">    res=&gt;&#123;</span><br><span class="line">      config.success(res);</span><br><span class="line">    &#125;</span><br><span class="line">  ).catch(<span class="function"><span class="params">err</span>=&gt;</span>&#123;</span><br><span class="line">      config.fail(res);</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//调用位置</span></span><br><span class="line"><span class="keyword">import</span> &#123;request&#125; <span class="keyword">from</span> <span class="string">&quot;./network/request/request&quot;</span>;</span><br><span class="line">request(&#123;</span><br><span class="line">  url:<span class="string">&#x27;comments&#x27;</span>,</span><br><span class="line">  success:<span class="function"><span class="params">res</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(res);</span><br><span class="line">  &#125;,</span><br><span class="line">  fail:<span class="function"><span class="params">err</span>=&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">  &#125;&#125;</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h5 id="最好的方法"><a href="#最好的方法" class="headerlink" title="最好的方法"></a>最好的方法</h5><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//封装位置</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&quot;axios&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">request</span>(<span class="params">config</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">let</span> newVar = axios.create(&#123;</span><br><span class="line">    baseURL:<span class="string">&quot;http://localhost:3008&quot;</span>,</span><br><span class="line">    timeout:<span class="number">5000</span></span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="keyword">return</span> newVar(config)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//调用位置</span></span><br><span class="line"><span class="keyword">import</span> &#123;request&#125; <span class="keyword">from</span> <span class="string">&quot;./network/request/request&quot;</span>;</span><br><span class="line">request(&#123;</span><br><span class="line">  url:<span class="string">&quot;comments&quot;</span></span><br><span class="line">&#125;).then(<span class="function"><span class="params">res</span>=&gt;</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(res);</span><br><span class="line">&#125;).catch(<span class="function"><span class="params">err</span>=&gt;</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(err);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>







    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E6%8A%80%E6%9C%AF/" rel="tag"># 技术</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/01/28/JavaScript/JavaScript/" rel="prev" title="JavaScript基础">
                  <i class="fa fa-chevron-left"></i> JavaScript基础
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/01/28/CSS/" rel="next" title="CSS基础">
                  CSS基础 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lawted Wu</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  


  <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>


<script>
var options = {
  bottom: '64px', // default: '32px'
  right: '32px', // default: '32px'
  left: 'unset', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: '#fff', // default: '#fff'
  backgroundColor: '#fff',  // default: '#fff'
  buttonColorDark: '#000',  // default: '#100f2c'
  buttonColorLight: '#fff', // default: '#fff'
  saveInCookies: true, // default: true,
  label: ' ', // default: ''
  autoMatchOsTheme: true // default: true
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
</script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
